package com.azure.bigstore.controller;

import com.azure.bigstore.entity.*;
import com.azure.bigstore.service.OrderItemService;
import com.azure.bigstore.service.OrderService;
import com.azure.bigstore.service.ProductService;
import com.azure.bigstore.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by liuhuaibin on 2018/1/22.
 */
@RestController
@RequestMapping("/v1")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private ProductService productService;
    @Autowired
    private TokenService tokenService;


    /**
     *
     * @return
     */
    @GetMapping("/order/{oid}")
    public Order getOrder(@PathVariable String oid){
        return orderService.getOrder(oid);
    }
    /**
     * 获取每个订单内的商品信息
     * @param oid
     * @return
     */
    @GetMapping("/orderitem/{oid}")
    public List<OrderItem> getOrderProducts(@PathVariable String oid){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        List<OrderItem> list = null;
        if(t != null && t != ""){
            Token token = tokenService.findByToken(t);
            //未查询到token
            if(token == null){
                response.setStatus(444);
            }else{
                if(token.getValidityTime().after(new Date())){
                    long time = new Date().getTime()+1800000;//延长30分钟
                    System.out.println("new:"+new Date()+"  time"+new Date(time));
                    token.setValidityTime(new Date(time));
                    tokenService.saveToken(token);

                    list = orderItemService.getItemsByOrderid(oid);
                    System.out.println("=======orderitemlist:"+list);

                }else{
                    response.setStatus(444);
                }
            }
        }else{
            response.setStatus(444);
        }
        return list;
    }

    /**
     * 获取用户的订单列表
     * @return
     */
    @GetMapping("/orderlist")
    public List<Order> getOrders(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        List<Order> list = null;
        if(t != null && t != ""){
            Token token = tokenService.findByToken(t);
            //未查询到token
            if(token == null){
                response.setStatus(444);
            }else{
                if(token.getValidityTime().after(new Date())){
                    long time = new Date().getTime()+1800000;//延长30分钟
                    System.out.println("new:"+new Date()+"  time"+new Date(time));
                    token.setValidityTime(new Date(time));
                    tokenService.saveToken(token);

                    list = orderService.getOrderList(token.getUid());
                }else{
                    response.setStatus(444);
                }
            }
        }else{
            response.setStatus(444);
        }
        return list;
    }

    @DeleteMapping("/order/{id}")
    public void deleteOrder(String id){
        //orderItemService.
    }
    @DeleteMapping("/orderlist")
    public void deleteOrders(){

    }
}
